package com.example.ykbboom.universitytransportation.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.MD5Util;
import com.example.ykbboom.universitytransportation.Util.NetUtil;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;
import com.example.ykbboom.universitytransportation.Util.ToastUtil;
import com.example.ykbboom.universitytransportation.VO.UserVO;

/**
 * @author ykbboom
 * @email ykbpro@whut.edu.cn
 * @time 2017-1-25 21:19:04
 * 功能: 登录
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText accountText;
    private EditText passwordText;
    private Button accountClearBtn;//清除账户名
    private Button passwordClearBtn;//清除密码
    private Button seePasswordBtn;//查看输入的密码
    private Button loginBtn;//登录
    private Button registerBtn;//注册
    private Button forgetPassBtn;//忘记密码
    private TextWatcher accountWatcher;//监控输入框
    private TextWatcher passwordWatcher;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if(SharedPrefUtil.getLoginState()){
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }
        initWatcher();
        initView();

    }
    public void initView(){
        accountText = (EditText) findViewById(R.id.account);
        passwordText = (EditText) findViewById(R.id.password);
        accountClearBtn = (Button)findViewById(R.id.account_clear);
        passwordClearBtn = (Button)findViewById(R.id.password_clear);
        seePasswordBtn = (Button)findViewById(R.id.password_eye);
        loginBtn = (Button)findViewById(R.id.login);
        registerBtn = (Button) findViewById(R.id.go_to_register);
        forgetPassBtn = (Button) findViewById(R.id.forget_password);
        accountText.addTextChangedListener(accountWatcher);//设置EditText监控事件
        passwordText.addTextChangedListener(passwordWatcher);//设置EditText监控事件
        accountClearBtn.setOnClickListener(this);
        passwordClearBtn.setOnClickListener(this);
        seePasswordBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        forgetPassBtn.setOnClickListener(this);
        progressDialog = new ProgressDialog(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.account_clear:
                accountText.setText("");
                passwordText.setText("");
                break;
            case R.id.password_clear:
                passwordText.setText("");
                break;
            case R.id.password_eye:
                if(passwordText.getInputType() == (InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD)){
                    seePasswordBtn.setBackgroundResource(R.drawable.close_eye);
                    passwordText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_NORMAL);
                }else{
                    seePasswordBtn.setBackgroundResource(R.drawable.open_eye);
                    passwordText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordText.setSelection(passwordText.getText().toString().length());
                break;
            case R.id.login:
                String account = accountText.getText().toString();
                String password = passwordText.getText().toString();
                if(account==null||password==null||account.trim().equals("")||password.trim().equals("")){
                        break;
                }else{
                    showDialog();
                    NetUtil.Login(account,MD5Util.encodeToHex(password),LoginActivity.this,progressDialog);
                    break;
                }

            case R.id.go_to_register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.forget_password:
                Intent intent1 = new Intent(LoginActivity.this,ForgetPassActivity.class);
                startActivity(intent1);
                break;
        }
    }
    /**
    * 监测输入框的输入状态
    * */
    private void initWatcher(){
        accountWatcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    accountClearBtn.setVisibility(View.VISIBLE);
                }else{
                    accountClearBtn.setVisibility(View.INVISIBLE);
                }
            }
        };
        passwordWatcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    passwordClearBtn.setVisibility(View.VISIBLE);
                }else{
                    passwordClearBtn.setVisibility(View.INVISIBLE);
                }
            }
        };
    }
    public void showDialog() {
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在登录...");
        progressDialog.show();
    }
}
